<?php
use \think\Db;
function redirect($url)
{
    header('Location:' . $url);
}
error_reporting(0);
function Admin_auth()
{
    $session = request()->session('uid');
    if (empty($session)) {
        redirect(url('/admin/login'));
        die;
    }
    $auth = session('auth');
    
    $action = request()->action();
    $controller = request()->controller();
    if ($action != "ajax_get" && $controller != "Material" && $action != 'ajax_set' && $controller . '/' . $action != 'Index/index' && $action != 'get_seller_category' && $action != 'get_seller') {
        $auth = explode(',', str_replace("|", ",", $auth));
        $check = in_array(strtolower($controller . '/' . $action), $auth);
        if (!$check) {
            die($controller . '/' . $action . 'error');
        }
    }
}

function pages($table, $where, $page = 1)
{
    $page = $page ? $page : 1;
    $list_rows = config('paginate.list_rows');
    $limit = ($page - 1) * $list_rows;
    $total = Db::table($table)->whereIn('aid', session('aid'))->where($where)->count();
    $pagetotal = ceil($total / $list_rows);
    return array('limit' => $limit . "," . $list_rows, 'pages' => ['page' => $page, 'pagetotal' => $pagetotal, 'total' => $total]);
}

function success($msg = '', $redirecturl = '')
{
    return view('public/success', ['msg' => $msg, 'url' => $redirecturl]);
}

function error($msg = '', $redirecturl = '')
{
    return view('public/error', ['msg' => $msg, 'url' => $redirecturl]);
}

function get_onlineip()
{
    $ch = curl_init('http://city.ip138.com/ip2city.asp');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $a = curl_exec($ch);
    preg_match('/\[(.*)\]/', $a, $ip);
    return $ip[1];
}

function seller_auth()
{
    $uid = session('wmo2o_u_uid');
    $type = session('wmo2o_u_type');
    $phone = session('wmo2o_u_phone');
    if (!empty($uid) && !empty($type) && !empty($phone)) {
        if (time() - session('wmo2o_u_time') > 3600) {
            session('wmo2o_u_uid', null);
            session('wmo2o_u_type', null);
            session('wmo2o_u_phone', null);
            session('wmo2o_u_time', null);
            redirect(url('/seller/login'));
            exit;
        } else {
            $user = Db::table('user')->where(['id' => $uid, 'phone' => $phone, 'type' => $type])->find();
            if (!$user) {
                redirect(url('/seller/login'));
                exit;
            }
            session('wmo2o_u_time', time());
            $config = Db::table('webconfig')->where('id', 1)->find();
            config('website', $config);
        }
    } else {
        redirect(url('/seller/login'));
        exit;
    }
}

function user_auth()
{
    $uid = session('wmo2o_u_uid');
    $type = session('wmo2o_u_type');
    $phone = session('wmo2o_u_phone');
    if (!empty($uid) && !empty($type) && !empty($phone)) {
        if (time() - session('wmo2o_u_time') > 3600) {
            session('wmo2o_u_uid', null);
            session('wmo2o_u_type', null);
            session('wmo2o_u_phone', null);
            session('wmo2o_u_time', null);
            redirect(url('/index/login'));
            exit;
        } else {
            $user = Db::table('user')->where(['id' => $uid, 'phone' => $phone, 'type' => $type])->find();
            if (!$user) {
                redirect(url('/index/login'));
                exit;
            }
            session('wmo2o_u_time', time());
        }
    } else {
        redirect(url('/index/login'));
        exit;
    }
}

function get_name($uid)
{
    return Db::table("seller")->where(['id' => $uid])->value('name');
}

function get_catname($cate_id)
{
    return Db::table("service_category")->where(['id' => $cate_id])->value('title');
}

use JPush\Client as JPush;


//推送
function push($uid, $parameters = ['type' => '', 'message' => '', 'parameters' => ''], $push_type)
{
    /*
     * message  消息
     * parameters 订单号或其他一些参数
     * type 0 = 系统后台推送消息, 1 = 提现结果, 2 = 订单提醒, 3 = 退款通知, 4 = 商家接单提醒 , 5 = 配送员抢单提醒, 6 = 私人订单（调度）
    */
    try {
        vendor('jpush.autoload');
        $app = Db::table('push_config')->where('type',$push_type)->find();
        $client = new JPush($app['appkey'], $app['appsecret']);
        $response = $client->push();
        if ($uid != '') {
            $response->addAlias($uid);
        } else {
            $response->addAllAudience();
        }
        $response->setPlatform(array('ios', 'android'))->message($parameters)->send();
        return true;
    } catch (\JPush\Exceptions\APIConnectionException $e) {
        return false;
    } catch (\JPush\Exceptions\APIRequestException $e) {
        return false;
    }
}

function insert_message($account_type = '', $uid, $title = '', $message = '', $type = '', $parameters = '')
{
    /*
     * title = 标题
     * type 0 = 系统后台推送消息, 1 = 提现结果, 2 = 订单提醒, 3 = 退款通知, 4 = 商家接单提醒 , 5 = 配送员抢单提醒, 6 = 私人订单（调度）
     * message  消息
     * parameters 订单号或其他一些参数
     */
    $time = time();
    Db::table('message')->insert(['account_type' => $account_type, 'uid' => $uid, 'title' => $title, 'message' => $message, 'type' => $type, 'parameters' => $parameters, 'time' => $time]);
}